package game

import (
	"MyWuXiaGame/server/src/csvs"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"math/rand"
	"os"
)

type ObjNpc struct {
	NpcId   int
	NpcName	string
	HP		int
	MP		int
	AttrInfo  map[int]int		    //Npc属性
	BoDie	bool
}

type ModNpc struct {
	player *Player
	path   string
}

func NewObjNpc(NpcId int) *ObjNpc {
	NpcConfig := csvs.GetNpcConfig(NpcId)
	if NpcConfig == nil {
		return nil
	}

	Npc := new(ObjNpc)
	Npc.NpcId = NpcId
	Npc.NpcName = NpcConfig.NpcName
	Npc.AttrInfo = make(map[int]int)
	Npc.CalcAbilitys(NpcConfig.JobAttr)
	Npc.HP = Npc.GetAttr(csvs.ATTR_MAXHP)
	Npc.MP = Npc.GetAttr(csvs.ATTR_MAXMP)
	Npc.BoDie = false

	return Npc
}

func (self *ObjNpc) CalcAbilitys(JobAttr csvs.TGrowthLineJobAttr) {
	if (self.AttrInfo != nil) && (JobAttr.JobAttrKeyValueList != nil) {
		for _, v := range JobAttr.JobAttrKeyValueList {
			self.AttrInfo[v.AttrIdx] = self.AttrInfo[v.AttrIdx] + v.AttrValue
		}
	}
}

func (self *ObjNpc) GetAttr(Attrtype int) int {
	if (Attrtype >= 1) && (Attrtype <= csvs.ATTR_MAXSIZE) {
		return self.AttrInfo[Attrtype]
	}

	return 0
}

func (self *ObjNpc) OnDamage(damage int) {
	if self.HP > damage {
		self.HP = self.HP - damage
	} else {
		self.HP = 0
		self.BoDie = true
	}
}

func (self *ModNpc) ObserveNpc(NpcId int) {
	config := csvs.GetNpcConfig(NpcId)
	if config != nil {
		fmt.Println(config.NpcName)
		fmt.Println("外貌看起来", config.AppearDescribe)
		fmt.Println("武艺看起来", config.StrengthDescribe)
		fmt.Println(config.ConcreteDescribe)
	}
}

func (self *ModNpc) ChatWithNpc(NpcId int) {
	ChatString := csvs.GetNpcChat(NpcId)

	fmt.Println(csvs.GetNpcName(NpcId), "：" , ChatString)
}

func (self *ModNpc) BattleWithNpc(NpcId int) {
	NpcConfig := csvs.GetNpcConfig(NpcId)
	if NpcConfig == nil {
		fmt.Println("没有该Npc")
		return
	}

	NpcObj := NewObjNpc(NpcId)
	if NpcObj == nil {
		fmt.Println("没有该Npc")
		return
	}

	self.player.GetModSkill().HandleViewAllSkill()

	ObjPlayer := self.player.GetModPlayer()
	ObjPlayer.ProcAddHPMP(ObjPlayer.GetAttr(csvs.ATTR_MAXHP), ObjPlayer.GetAttr(csvs.ATTR_MAXMP))

	for {
		fmt.Println(NpcObj.NpcName, "当前的血量：", NpcObj.HP)
		fmt.Println("你当前的血量：", ObjPlayer.CurHP())
		fmt.Println("请选择需要施展的武学： 0逃跑")
		var skillid int
		fmt.Scan(&skillid)
		if skillid == 0 {
			return
		}

		//玩家攻击
		LearnedSkill := self.player.GetModSkill().LearnedSkillInfo[skillid]
		if LearnedSkill == nil {
			fmt.Println("未学会该武学")
			continue
		}
		SkillConfig := csvs.ConfigSkillLevelMap[LearnedSkill.SkillId][LearnedSkill.SkillLv]
		if SkillConfig == nil {
			fmt.Println("该武学不存在")
			continue
		}

		lvDCNum := ObjPlayer.GetAttr(csvs.ATTR_MAXDC) - ObjPlayer.GetAttr(csvs.ATTR_DC)
		damage := 0
		if lvDCNum == 0 {
			damage = SkillConfig.BaseDamage + ObjPlayer.GetAttr(csvs.ATTR_DC)
		} else {
			rand1 := rand.Intn(lvDCNum) + ObjPlayer.GetAttr(csvs.ATTR_DC)
			damage = SkillConfig.BaseDamage + rand1
		}

		//武学描述
		SkillDescribe, HitDescribe, FeelDescribe := csvs.GetSkillDescribe(SkillConfig.SkillID)
		fmt.Println("你施展出了", SkillConfig.SkillName, " ", SkillDescribe, " ", HitDescribe, " ", NpcObj.NpcName, FeelDescribe)
		fmt.Println("造成了", damage, "点伤害")

		NpcObj.OnDamage(damage)
		if NpcObj.BoDie {
			fmt.Println("你获得了胜利")
			break
		}

		//Npc攻击
		NpcSkillId, NpcSkilllv := csvs.GetNpcSkillId(NpcObj.NpcId)
		SkillConfig = csvs.ConfigSkillLevelMap[NpcSkillId][NpcSkilllv]
		if SkillConfig == nil {
			fmt.Println("该武学不存在")
			continue
		}

		lvDCNum = NpcObj.GetAttr(csvs.ATTR_MAXDC) - NpcObj.GetAttr(csvs.ATTR_DC)
		damage = 0
		if lvDCNum == 0 {
			damage = SkillConfig.BaseDamage + NpcObj.GetAttr(csvs.ATTR_DC)
		} else {
			rand1 := rand.Intn(lvDCNum) + NpcObj.GetAttr(csvs.ATTR_DC)
			damage = SkillConfig.BaseDamage + rand1
		}

		//武学描述
		SkillDescribe, HitDescribe, FeelDescribe = csvs.GetSkillDescribe(SkillConfig.SkillID)
		fmt.Println(NpcObj.NpcName, "施展出了", SkillConfig.SkillName, " ", SkillDescribe, " ", HitDescribe, " ", NpcObj.NpcName, FeelDescribe)
		fmt.Println("造成了", damage, "点伤害")

		ObjPlayer.ProcDecHPMP(damage, 0)

		if ObjPlayer.BoDie {
			fmt.Println("你被痛扁了一顿后落荒而逃")
			break
		}
	}
}

func (self *ModNpc) SaveData() {
	content, err := json.Marshal(self)
	if err != nil {
		return
	}
	err = ioutil.WriteFile(self.path, content, os.ModePerm)
	if err != nil {
		return
	}
}

func (self *ModNpc) LoadData(player *Player) {

	self.player=player
	self.path=self.player.localPath+"/npc.json"

	configFile, err := ioutil.ReadFile(self.path)
	if err != nil {
		self.InitData()
		return
	}
	err = json.Unmarshal(configFile, &self)
	if err != nil {
		self.InitData()
		return
	}

	return
}

func (self *ModNpc) InitData() {

}